package org.minuframework.lang;

import java.util.List;

public class StringUtils {

    public static final String DEFAULT_UPPER_ARRAY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static final String DEFAULT_LOWER_ARRAY = "abcdefghijklmnopqrstuvwxyz";

    public static final String DEFAULT_NUM_ARRAY = "1234567890";

    public static final char UNDERLINE = '_';

    public static final char PERIOD = '.';

    /**
     * 检测字符串串否为空
     * 
     * @param param 字符串 String
     * @return boolean
     */
    public static boolean isEmpty(String param) {
        return param == null || "".equals(param) || param.length() <= 0;
    }

    /**
     * 检测字符串串否不为空
     * 
     * @param param 字符串 String
     * @return boolean
     */
    public static boolean isNotEmpty(String param) {
        return !isEmpty(param);
    }

    /**
     * 移除字符串两侧空格
     * 
     * @param param 字符串 Object
     * @return String
     */
    public static String trim(Object param) {
        if (param == null) {
            return null;
        }
        return trim(param.toString());
    }

    /**
     * 移除字符串两侧空格
     * 
     * @param param 字符串 String
     * @return String
     */
    public static String trim(String param) {
        return trimRight(trimLeft(param));
    }

    /**
     * 移除字符串左侧空格
     * 
     * @param param 字符串 Object
     * @return String
     */
    public static String trimLeft(Object param) {
        if (param == null) {
            return null;
        }
        return trimLeft(param.toString());
    }

    /**
     * 移除字符串左侧空格
     * 
     * @param param 字符串 String
     * @return String
     */
    public static String trimLeft(String param) {
        if (isEmpty(param)) {
            return param;
        }

        int len = 0;
        while ((len < param.length()) && (param.toCharArray()[len] <= ' ')) {
            len++;
        }

        return param.substring(len, param.length());
    }

    /**
     * 移除字符串右侧空格
     * 
     * @param param 字符串 Object
     * @return String
     */
    public static String trimRight(Object param) {
        if (param == null) {
            return null;
        }
        return trimRight(param.toString());
    }

    /**
     * 移除字符串右侧空格
     * 
     * @param param 字符串 String
     * @return String
     */
    public static String trimRight(String param) {
        if (isEmpty(param)) {
            return param;
        }

        int len = param.length();
        while ((len > -1) && (param.toCharArray()[len - 1] <= ' ')) {
            len--;
        }

        return param.substring(0, len);
    }

    /**
     * 移除字符串中所有空格
     * 
     * @param param 字符串 Object
     * @return String
     */
    public static String trimAll(Object param) {
        if (param == null) {
            return null;
        }
        return trimAll(param.toString());
    }

    /**
     * 移除字符串中所有空格
     * 
     * @param param 字符串 String
     * @return String
     */
    public static String trimAll(String param) {
        if (isEmpty(param)) {
            return param;
        }

        StringBuilder result = new StringBuilder();
        char[] arr = param.toCharArray();
        int len = 0;
        while ((len < arr.length)) {
            if (arr[len] > ' ') {
                result.append(arr[len]);
            }
            len++;
        }

        return result.toString();
    }

    /**
     * 大标题
     * 
     * @param param 字符串 String
     * @return String
     */
    public static String title(String param) {
        return "======================= " + param + " =======================";
    }

    /**
     * 小标题
     * 
     * @param param 字符串 String
     * @return String
     */
    public static String subTitle(String param) {
        return "----------------------- " + param + " -----------------------";
    }

    /**
     * 检测是否为字符
     * 
     * @param param 字符
     * @return boolean
     */
    public static boolean isCharacter(char param) {
        return isUpperCase(param) || isLowerCase(param);
    }

    /**
     * 检测是否为字符串
     * 
     * @param param 字符串
     * @return boolean
     */
    public static boolean isString(char param) {
        return isUpperCase(param) || isLowerCase(param);
    }

    /**
     * 检测是否为大写字母
     * 
     * @param param 字符
     * @return boolean
     */
    public static boolean isUpperCase(char param) {
        String str = String.valueOf(param);
        if (isEmpty(str)) {
            return false;
        }

        char[] array = DEFAULT_UPPER_ARRAY.toCharArray();
        List<Character> list = ArrayUtils.asList(array);
        for (char c : str.toCharArray()) {
            if (!list.contains(c)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 检测是否为大写字母
     * 
     * @param param 字符串
     * @return boolean
     */
    public static boolean isUpperCase(String param) {
        if (isEmpty(param)) {
            return false;
        }

        char[] array = DEFAULT_UPPER_ARRAY.toCharArray();
        List<Character> list = ArrayUtils.asList(array);
        for (char c : param.toCharArray()) {
            if (!list.contains(c)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 检测是否为小写字母
     * 
     * @param param 字符
     * @return boolean
     */
    public static boolean isLowerCase(char param) {
        String str = String.valueOf(param);
        if (isEmpty(str)) {
            return false;
        }

        char[] array = DEFAULT_LOWER_ARRAY.toCharArray();
        List<Character> list = ArrayUtils.asList(array);
        for (char c : str.toCharArray()) {
            if (!list.contains(c)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 检测是否为小写字母
     * 
     * @param param 字符串
     * @return boolean
     */
    public static boolean isLowerCase(String param) {
        if (isEmpty(param)) {
            return false;
        }

        char[] array = DEFAULT_LOWER_ARRAY.toCharArray();
        List<Character> list = ArrayUtils.asList(array);
        for (char c : param.toCharArray()) {
            if (!list.contains(c)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 检测是否为数字
     * 
     * @param param 字符串
     * @return boolean
     */
    public static boolean isNumCase(char param) {
        boolean flag = true;

        try {
            Integer.parseInt(String.valueOf(param));
        } catch (NumberFormatException e) {
            flag = false;
        }

        return flag;
    }

    /**
     * 检测是否为数字
     * 
     * @param param 字符串
     * @return boolean
     */
    public static boolean isNumCase(String param) {
        boolean flag = true;

        try {
            Integer.parseInt(param);
        } catch (NumberFormatException e) {
            flag = false;
        }

        return flag;
    }

    /**
     * 检测是否为下划线
     * 
     * @param c 字符串
     * @return boolean
     */
    public static boolean isUnderline(char c) {
        return c == UNDERLINE;
    }

    /**
     * 检测是否为句号
     * 
     * @param c 字符
     * @return boolean
     */
    public static boolean isPeriod(char c) {
        return c == PERIOD;
    }

    /**
     * 字母转小写
     * 
     * @param param 字符
     * @return char
     */
    public static char toLowerCase(char param) {
        return Character.toLowerCase(param);
    }

    /**
     * 字母转小写
     * 
     * @param param 字符
     * @return String
     */
    public static String toLowerCase(String param) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (isCharacter(c)) {
                result.append(upperToLower(c));
            } else if (isNumCase(c)) {
                result.append(c);
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 字母转大写
     * 
     * @param param 字符
     * @return char
     */
    public static char toUpperCase(char param) {
        return Character.toUpperCase(param);
    }

    /**
     * 字母转大写
     * 
     * @param param 字符
     * @return String
     */
    public static String toUpperCase(String param) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (isCharacter(c)) {
                result.append(upperToLower(c));
            } else if (isNumCase(c)) {
                result.append(c);
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 大写字母转小写
     * 
     * @param param 字符
     * @return char
     */
    public static char upperToLower(char param) {
        return Character.toLowerCase(param);
    }

    /**
     * 大写字母转小写
     * 
     * @param param 字符
     * @return String
     */
    public static String upperToLower(String param) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (isCharacter(c)) {
                result.append(upperToLower(c));
            } else if (isNumCase(c)) {
                result.append(c);
            } else {
                result.append(UNDERLINE);
            }
        }

        return result.toString();
    }

    /**
     * 大写字母转小写
     * 
     * @param param 字符
     * @param symbol 需替换符号
     * @return String
     */
    public static String upperToLower(String param, char symbol) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (c == symbol) {
                result.append(UNDERLINE);
            } else if (isCharacter(c)) {
                result.append(upperToLower(c));
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 大写字母转小写
     * 
     * @param param 字符
     * @param replace 需替换符号
     * @param symbol 替换符号
     * @return String
     */
    public static String upperToLower(String param, char replace, char symbol) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (c == replace) {
                result.append(symbol);
            } else if (isCharacter(c)) {
                result.append(upperToLower(c));
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 小写字母转大写
     * 
     * @param param 字符
     * @return char
     */
    public static char lowerToUpper(char param) {
        return Character.toUpperCase(param);
    }

    /**
     * 小写字母转大写
     * 
     * @param param 字符
     * @return String
     */
    public static String lowerToUpper(String param) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (isCharacter(c)) {
                result.append(lowerToUpper(c));
            } else if (isNumCase(c)) {
                result.append(c);
            } else {
                result.append(UNDERLINE);
            }
        }

        return result.toString();
    }

    /**
     * 小写字母转大写
     * 
     * @param param 字符
     * @param symbol 需替换符号
     * @return String
     */
    public static String lowerToUpper(String param, char symbol) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (c == symbol) {
                result.append(UNDERLINE);
            } else if (isCharacter(c)) {
                result.append(lowerToUpper(c));
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 小写字母转大写
     * 
     * @param param 字符
     * @param replace 需替换符号
     * @param symbol 替换符号
     * @return String
     */
    public static String lowerToUpper(String param, char replace, char symbol) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (c == replace) {
                result.append(symbol);
            } else if (isCharacter(c)) {
                result.append(lowerToUpper(c));
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 驼峰转下划线
     * 
     * @param param 字符串
     * @return String
     */
    public static String camelToUnderline(String param) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (char c : chars) {
            if (isUpperCase(c)) {
                result.append(UNDERLINE).append(upperToLower(c));
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 驼峰转下划线
     * 
     * @param param 字符串
     * @return String
     */
    public static String underlineToCamel(String param) {
        StringBuilder result = new StringBuilder();
        if (isEmpty(param)) {
            return result.toString();
        }

        char[] chars = param.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (isUnderline(chars[i])) {
                ++i;
                result.append(lowerToUpper(chars[i]));
            } else {
                result.append(chars[i]);
            }
        }

        return result.toString();
    }

    /**
     * 比较连个字符串是否相同
     * 
     * @param param 字符串
     * @param compare 字符串
     * @return boolean
     */
    public static boolean equals(String param, String compare) {
        if (isEmpty(param) || isEmpty(compare)) {
            return false;
        }
        return param.equalsIgnoreCase(compare);
    }

    public static void main(String[] args) {
        System.out.println(trim(null));
    }

}
